/*
* Autocompletion class
* Borrowed from smaps.co.nz -thx :)
*/
Autocomplete = Class.create();
Autocomplete.prototype = {
	initialize: function (element, container) {
		this.element = $(element);
		this.container = $(container);
		this.element.setAttribute("autocomplete", "off");

		this.more_results_selected = false;
		this.last_value = "";
		this.list_nodes = [];
		this.hasFocus = false;
		this.current_selection_index = -1;
		this.previous_selection_index = this.current_selection_index;

		Event.observe(this.element, 'keyup', this.keyPressed.bindAsEventListener(this), false);
		Event.observe(this.element, 'blur', this.inputLostFocus.bindAsEventListener(this), false);
		Event.observe(this.element, 'focus', this.inputGotFocus.bindAsEventListener(this), false);
	},

	sanitizeQuery: function (query) {
		var sanitized_query = query;
		sanitized_query = sanitized_query.replace(/[\&]/g,'');
		return escape(sanitized_query);
	},

	closeList: function () {
		this.list_nodes = [];
		this.current_selection_index = -1;
		this.previous_selection_index = this.current_selection_index;
		this.container.style.display = "none";
	},

	listNodeClicked: function (e) {
		var node = Event.element(e);
		var url = node.getAttribute('url');
		if (!url) {
			node = node.parentNode;
		}
		url = node.getAttribute('url');
		var saved_last_value = this.last_value;
		this.last_value = node.innerHTML.replace(/<.*?>/g, '');

		if (url == "more_results") {
			this.more_results_selected = true;
			this.last_value = saved_last_value;
		}
		this.element.value = this.last_value;
		//this.processSubmit();
	},

	listNodeFocusedWithMouse: function (e) {
		var node = Event.element(e);
		var url = node.getAttribute('url');
		if (!url) {
			node = node.parentNode;
		}
		var node_index = this.list_nodes.indexOf(node);

		this.previous_selection_index = this.current_selection_index;
		if (this.previous_selection_index >= 0 && this.previous_selection_index < this.list_nodes.length) {
			this.list_nodes[this.previous_selection_index].className = "normal";
		}
		this.current_selection_index = node_index;
		this.list_nodes[this.current_selection_index].className = "focused";
	},

	inputLostFocus: function (e) {
		this.last_value = "";
		this.hasFocus = false;
		this.closeList();
	},
	
	inputGotFocus: function (e) {
		this.hasFocus = true;
	},

	keyPressed: function (e) {
		switch (e.keyCode) {
		case Event.KEY_ESC:
			this.element.value = this.last_value;
			this.last_value = "";
			this.closeList();
			return;
			break;
		case Event.KEY_RETURN:
			this.closeList();
			return;
			break;
		case Event.KEY_LEFT:
		case Event.KEY_RIGHT:
			return;
			break;
		case Event.KEY_UP:
		case Event.KEY_DOWN:
			if (this.container.style.display == "none") {
				return;
			}
			this.previous_selection_index = this.current_selection_index;
			if (this.previous_selection_index >= 0 && this.previous_selection_index < this.list_nodes.length) {
				this.list_nodes[this.previous_selection_index].className = "normal";
			}

			switch (e.keyCode) {
				case Event.KEY_UP:
				this.current_selection_index--;
				if (this.current_selection_index < 0) {
					this.current_selection_index = -1;
					this.element.value = this.last_value;
				}
				break;
				case Event.KEY_DOWN:
				this.current_selection_index++;
				if (this.current_selection_index >= this.list_nodes.length) {
					this.current_selection_index = this.list_nodes.length - 1;
				}
				break;
			}

			if (this.current_selection_index >= 0 && this.current_selection_index < this.list_nodes.length) {
				var selected_node = this.list_nodes[this.current_selection_index];
				selected_node.className = "focused";
				var url = selected_node.getAttribute('url');
				if (url == "more_results") {
					this.element.value = this.last_value;
					this.more_results_selected = true;
				}
				else {
					this.element.value = selected_node.innerHTML.replace(/<.*?>/g, '');
				}
			}
			return;
			break;
		}
		this.watchKeyStrokes();
	},

	ajaxRequestCompleted: function (originating_request) {
		this.container.innerHTML = originating_request.responseText;
		this.list_nodes = $A(this.container.getElementsByTagName('div'));
		if (this.list_nodes.length && this.hasFocus) {
			for (var i = 0; i < this.list_nodes.length; i++) {
				var node = this.list_nodes[i];
				node.onmousedown = this.listNodeClicked.bindAsEventListener(this);
				node.onmouseover = this.listNodeFocusedWithMouse.bindAsEventListener(this);
			}
			this.container.style.display = "block";
		}
		else {
			this.closeList();
		}

		this.loading = false;
		if (this.element.value != this.last_value) {
			this.watchKeyStrokes();
		}
	},

	watchKeyStrokes: function () {
		if (this.loading) {
			return;
		}
		if (this.element.value == "") {
			this.closeList();
			return;
		}

		this.loading = true;
		this.last_value = this.element.value;

		var url = 'autocomplete.php';
		var params = 'r=' + (($('fregionEU').checked == true) ? $('fregionEU').value : $('fregionUS').value) + '&s=' + this.sanitizeQuery(this.element.value);
		var ajax_request = new Ajax.Request(url, {
				method: 'get',
				parameters: params,
				onComplete: this.ajaxRequestCompleted.bind(this)
			}
		);
	}
}